package com.hbue.water.controller;

import com.github.pagehelper.PageInfo;
import com.hbue.water.common.CommonResult;
import com.hbue.water.common.ResponseCode;
import com.hbue.water.entity.Customer;
import com.hbue.water.service.CustomerService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yushu
 * @date 2021/12/27
 * @apiNate
 */
@RestController
@RequestMapping("/cust")
@Slf4j
@ApiModel("客户管理Controller")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @ApiOperation("分页查询所有客户信息")
    @GetMapping("listCustPage/{pageNum}/{pageSize}")
    public CommonResult<PageInfo> listCustomer(
            @ApiParam(name = "pageNum", value = "当前页", required = true)
            @PathVariable("pageNum") Integer pageNum,
            @ApiParam(name = "pageSize", value = "每页数据量", required = true)
            @PathVariable("pageSize") Integer pageSize){
        PageInfo<Customer> customerPageInfo = customerService.listCustomer(pageNum, pageSize);
        return new CommonResult<>(ResponseCode.RESPONSE_SUCCESS, "success", customerPageInfo);
    }

    @ApiOperation("展示所有客户信息")
    @GetMapping("/listCust")
    public CommonResult<List<Customer>> listCustomer() {
        List<Customer> custList = customerService.list();
        return new CommonResult<>(ResponseCode.RESPONSE_SUCCESS, "ok", custList);
    }

    @ApiOperation("添加客户信息")
    @PostMapping("/saveCust")
    public CommonResult<Boolean> saveCustomer(
            @ApiParam(name = "customer", value = "前端采集的客户信息", required = true)
            @RequestBody Customer customer) {
        boolean flag = customerService.save(customer);
        if (flag) {
            return new CommonResult<>(ResponseCode.RESPONSE_SUCCESS, "OK", true);
        } else {
            return new CommonResult<>(ResponseCode.RESPONSE_FAIL, "Fail", false);
        }
    }

    @ApiOperation("根据用户ID删除客户信息")
    @GetMapping("/deleteCust/{cid}")
    public CommonResult<Boolean> deleteCustomer(
            @ApiParam(name = "cid", value = "客户id", required = true)
            @PathVariable("cid") Integer cid){
        boolean flag = customerService.removeById(cid);
        if (flag) {
            return new CommonResult<>(ResponseCode.RESPONSE_SUCCESS, "ok", true);
        } else {
            return new CommonResult<>(ResponseCode.RESPONSE_FAIL, "fail", false);
        }
    }

    @ApiOperation("根据客户ID查询客户信息，数据回显")
    @GetMapping("/getCustById/{cid}")
    public CommonResult<Customer> getCustomerById(
            @ApiParam(name = "cid", value = "客户ID", required = true)
            @PathVariable("cid") Integer cid){
        Customer customer = customerService.getById(cid);
        // return new CommonResult<>(ResponseCode.RESPONSE_SUCCESS, "ok", customer);
        if (null == customer){
            return new CommonResult<>(ResponseCode.RESPONSE_FAIL, "null", null);
        } else {
            return new CommonResult<>(ResponseCode.RESPONSE_SUCCESS, "ok", customer);
        }
    }

    @ApiOperation("修改客户信息")
    @PostMapping("/updateCust")
    public CommonResult<Customer> updateCustomer(
            @ApiParam(name = "customer", value = "前端采集的客户信息", required = true)
            @RequestBody Customer customer){
        boolean flag = customerService.updateById(customer);
        if (flag) {
            return new CommonResult<>(ResponseCode.RESPONSE_SUCCESS, "ok", customer);
        } else {
            return new CommonResult<>(ResponseCode.RESPONSE_FAIL, "fail", customer);
        }
    }

    @ApiOperation("根据搜索条件查找客户信息")
    @GetMapping("/searchCustomer/{pageNum}/{pageSize}/{searchCondition}")
    public CommonResult<PageInfo> searchCustomer(
            @ApiParam(name="pageNum",value="当前页",required = true)
            @PathVariable("pageNum") Integer pageNum,
            @ApiParam(name = "pageSize", value = "每页数据量", required = true)
            @PathVariable("pageSize") Integer pageSize,
            @ApiParam(name = "searchCondition", value = "搜索条件", required = true)
            @PathVariable String searchCondition){
        log.info("search searchCondition = "+searchCondition);
        PageInfo<Customer> pageInfo = customerService.searchCustomer(pageNum, pageSize, searchCondition);
        return new CommonResult<>(ResponseCode.RESPONSE_SUCCESS, "ok", pageInfo);
    }


}